//
//  LoginViewController.m
//  LQH-Douban
//
//  Created by lanou3g on 15/10/22.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginView.h"
#import "RegisterViewController.h"
#import <AVUser.h>

@interface LoginViewController ()

@property (strong, nonatomic) LoginView *rv;

@property (nonatomic, copy) NSString *userName;
@property (nonatomic, copy) NSString *password;

@end

@implementation LoginViewController

- (void)viewWillAppear:(BOOL)animated
{
    // 注册成功后自动登录
    if ([AVUser currentUser]) {
        
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
    }
}

- (void)loadView
{
    self.rv = [[LoginView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    self.view = self.rv;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"登陆";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"< 返回" style:(UIBarButtonItemStylePlain) target:self action:@selector(leftBarButtonAction:)];
    
    [self.rv.loginButton addTarget:self action:@selector(loginAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.rv.registerButton addTarget:self action:@selector(registerAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (void)leftBarButtonAction:(UIBarButtonItem *)sender
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)loginAction:(UIButton *)sender
{
    
    
    [AVUser logInWithUsernameInBackground:self.rv.userNameTf.text password:self.rv.passwordTf.text block:^(AVUser *user, NSError *error) {
       
        if (user) {
            
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];
            
        } else {
            
            int errorCode = [(NSNumber *)error.userInfo[@"code"] intValue] ;

            if (errorCode == 211) {
                
                [self p_showAlertWithMessage:@"用户不存在!"];
            } else if (errorCode == 210) {
                
                [self p_showAlertWithMessage:@"密码不正确!"];
            }
            
        }
        
    }];
}

- (void)registerAction:(UIButton *)sender
{
    RegisterViewController *registerVC = [[RegisterViewController alloc] init];
    
    registerVC.login = ^(NSString *username, NSString *password){
    
        
        [AVUser logInWithUsernameInBackground:username password:password block:^(AVUser *user, NSError *error) {
            
            if (user != nil) {
                NSLog(@"登录成功");
                [self dismissViewControllerAnimated:YES completion:^{
                    
                }];
            } else {
                
                int errorCode = [(NSNumber *)error.userInfo[@"code"] intValue] ;
                
                if (errorCode == 211) {
                    
                    [self p_showAlertWithMessage:@"用户不存在!"];
                } else if (errorCode == 210) {
                    
                    [self p_showAlertWithMessage:@"密码不正确!"];
                }
            }
        }];
    };
    
    [self showViewController:registerVC sender:nil];
    
    
}

- (void)p_showAlertWithMessage:(NSString *)message
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"⚠️警告" message:message preferredStyle:(UIAlertControllerStyleAlert)];
    
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alert addAction:cancel];
    
    [self presentViewController:alert animated:YES completion:^{
        
    }];
    
}

@end
